package com.samba.admin;

import java.util.Collection;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.samba.entites.Type;
import com.samba.sessions.AdminServiceDAO;
  
@ManagedBean(name="typeManager")
@SessionScoped
public class TypeManagerBean {
	
	@EJB()
	private AdminServiceDAO adminService;
	
	private Type managed;
	

	/**
	 * create a new instance of TypeManagerBean
	 */
	
	public TypeManagerBean(){
		managed = new Type();
	}
	
	public void initTypeFromRequestParam()
	{
		managed = adminService.findTypeById(getManagedTypeId());
	}

	public int getManagedTypeId()
	{

		ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		String param = request.getParameter("id");
		return Integer.parseInt(param);
	}
	
	public Collection getList(){
		return adminService.findAllTypes();
	}
	public void detail ()
	{
	}
	public void remove()
	{
		adminService.deleteType(getManagedTypeId());
	}
	public void edit()
	{
		initTypeFromRequestParam();
	}
	public void newBlank()
	{
		managed = new Type();
	}
	public void save()
	{
		if(managed.getId() > 0)
		{
			adminService.updateType(managed);
		}else
		{
			adminService.addType(managed);
			newBlank();
		}
	}
	public Type getManaged() {
		return managed;
	}

	public void setManaged(Type managed) {
		this.managed = managed;
	}
}
